package com.cskaoyan.service.wx;

import com.cskaoyan.bean.MarketComment;
import com.cskaoyan.bean.MarketCommentExample;
import com.cskaoyan.bean.MarketUser;
import com.cskaoyan.mapper.MarketCommentMapper;
import com.cskaoyan.mapper.MarketUserMapper;
import com.cskaoyan.model.admin.vo.BaseDataListVo;
import com.cskaoyan.model.vx.bo.WxCommentPostBo;
import com.cskaoyan.model.vx.vo.WxCommentCountVo;
import com.cskaoyan.model.vx.vo.WxCommentVo;
import com.cskaoyan.model.vx.vo.WxUserInfoVo;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author hanshuai
 * @version 1.0
 * @description @TODO
 * @date 2021/12/2 15:11
 */
@Service
public class WxCommentServiceImpl implements WxCommentService {
    @Autowired
    MarketCommentMapper marketCommentMapper;
    @Autowired
    MarketUserMapper marketUserMapper;

    @Override
    public BaseDataListVo commentList(Integer page, Integer limit, Integer valueId, Byte type, Boolean showType, HttpSession httpSession) {
        PageHelper.startPage(page, limit);
        MarketCommentExample example = new MarketCommentExample();
        MarketCommentExample.Criteria criteria = example.createCriteria();
        criteria.andValueIdEqualTo(valueId).andTypeEqualTo(type).andDeletedEqualTo(false);
        if (showType == true) {
            criteria.andHasPictureEqualTo(showType);
        }
        List<MarketComment> marketComments = marketCommentMapper.selectByExample(example);
//        List<WxCommentVo> wxCommentVos = new ArrayList<>();
        PageInfo pageInfo = new PageInfo(marketComments);
        BaseDataListVo baseDataListVo = new BaseDataListVo();
        baseDataListVo.setList(marketComments);
        baseDataListVo.setLimit(limit);
        baseDataListVo.setPage(1);
        baseDataListVo.setPages(pageInfo.getPages());
        baseDataListVo.setTotal((int) pageInfo.getTotal());
        return baseDataListVo;
    }

    @Override
    public MarketComment commentPost(WxCommentPostBo wxCommentPostBo, HttpSession httpSession) {
        MarketComment marketComment = new MarketComment();
        Date date = new Date();
        marketComment.setUpdateTime(date);
        marketComment.setAddTime(date);
        marketComment.setContent(wxCommentPostBo.getContent());
        marketComment.setId(null);
        marketComment.setValueId(Integer.parseInt(wxCommentPostBo.getValueId()));
        marketComment.setHasPicture(wxCommentPostBo.getHasPicture());
        marketComment.setDeleted(false);
        marketComment.setType(wxCommentPostBo.getType());


        marketComment.setUserId((Integer) httpSession.getAttribute("wxUserId"));


        marketCommentMapper.insertSelective(marketComment);
        return marketComment;
    }

    @Override
    public WxCommentCountVo commentCount(Integer valueId, Byte type) {
        MarketCommentExample example = new MarketCommentExample();
        MarketCommentExample.Criteria criteria = example.createCriteria().andValueIdEqualTo(valueId).andTypeEqualTo(type);
        Integer allCount = (int) marketCommentMapper.countByExample(example);
        criteria.andHasPictureEqualTo(true);
        Integer hasPicCount = (int) marketCommentMapper.countByExample(example);

        WxCommentCountVo wxCommentCountVo = new WxCommentCountVo();
        wxCommentCountVo.setAllCount(allCount);
        wxCommentCountVo.setHasPicCount(hasPicCount);
        return wxCommentCountVo;
    }
}
